var ArrayUtil={
    
    remove:function(arr, val ){
        for (var i = arr.length - 1; i >= 0; i--) {
            if(arr[i] == val )arr.splice(i, 1);
        }
        return arr ; 
    },
    removes:function(arr,vals){
        for (var i = vals.length - 1; i >= 0; i--) {
           arr = this.remove(arr,vals[i]);
        }
        return arr ; 
    },
    arrayRemove:function (arr,key,obj) {
            var isArr = Array.isArray(key);
            for(var i = arr.length - 1;  i >=0; i --){
                if(isArr){

                    var f = true;
                    for(var j = 0;  j < key.length ; j ++){

                        if(arr[i][key[j]] != obj[key[j]]){
                            f = false;
                            break;
                        }
                    }
                    if(f)arr.splice(i,1);

                }else{

                    if(arr[i][key] == obj[key])arr.splice(i,1);

                }
                
            }
            return arr; 
    },
    arrayRemoveAll:function (arr,key,objs) {
            
            for(var i = 0 ; i <objs.length ; i++){
                arr = this.arrayRemove(arr,key,objs[i]);
            }
            return arr; 
    },
    arrayRemoveForKeyValue:function(arr ,key,value ){

        for(var i = arr.length -1 ; i >=0 ; i --){
            if(arr[i][key] == value)arr.splice(i,1);
        }
        return arr;
    },
    arrayRemoveForKeys:function (arr, key1 , obj ,key2 ){


            for(var i = arr.length -1 ; i >=0 ; i--){

                if(arr[i][key1] == obj[key2])arr.splice(i,1);
            }
            return arr ; 

    },
    arrayRemoveAllForKeys:function (arr,key1,objs,key2 ){

            for(var i = 0 ; i <objs.length ; i ++){
               arr = this.arrayRemoveForKeys(arr,key1,objs[i],key2);
            }
            return arr;
    },
    arrayAddForKeys:function (arr,key1,obj,key2 ){

            for(var i = 0 ; i<arr.length ; i ++){
                if(arr[i][key1] == obj[key2])return arr;
            }
            arr.push(obj);
            return arr;
    },
    arrayAddAllForKeys:function (arr,key1,objs,key2 ){

            for(var i = 0 ; i <objs.length ; i ++){
               arr = this.arrayAddForKeys(arr,key1,objs[i],key2);
            }
            return arr;
    },
    arrayAdd:function (arr ,key ,obj ){
            var isArr = Array.isArray(key);
            for(var i= 0 ; i <arr.length ;i++){
                if(isArr){
                    var f = true  ;
                    for(var j = 0 ; j <key.length ; j ++){

                        if(arr[i][key[j]] != obj[key[j]]){
                            f = false;
                            break;
                        }
                    }

                    if(f){
                        return arr;
                    }

                }else{
                    if(arr[i][key] == obj[key]) {

                        return arr ;
                    }
                }
            }
            arr.push(obj);
            return arr ; 
    },
   arrayAddAll:function (arr,key,objs){

            for(var i = 0 ; i<objs.length ; i ++){
                arr = this.arrayAdd(arr,key,objs[i]);
            }
            return arr;
    },
    arrayCopyAdd:function (arr ,key ,obj ){
            var isArr = Array.isArray(key);
            var tmp = [];
            for(var i= 0 ; i <arr.length ;i++){

                if(isArr){
                    var f = true  ;
                    for(var j = 0 ; j <key.length ; j ++){

                        if(arr[i][key[j]] != obj[key[j]]){
                            f = false;
                            break;
                        }
                    }
                      if(f)continue;

                }else{
                    if(arr[i][key] == obj[key]) {
                        continue;
                        
                    }
                }

                tmp.push(arr[i]);
            }
            tmp.push(obj);
            return tmp ; 
    },
    arrayCopyAddAll:function(arr,key,objs){

        for(var i = 0 ; i<objs.length ; i ++){
                arr = this.arrayCopyAdd(arr,key,objs[i]);
            }
            return arr;
    },

    arrayGetByKeysValue:function(arr, keys,row){
        var t = [];
        for(var i = 0 ; i<arr.length ; i ++){
            var f =true ;
            for(var j = 0 ;j < keys.length ; j ++){
                
                var key = keys[j];
                if(arr[i][key] != row[key]){
                    f = false;
                    break;
                }
               
            }
             if(f)t.push(arr[i]);
            
        }
        return t ;

    },
    arrayGetByKeyValue:function(arr,key,value){
        var t = [];
        for(var i = 0 ; i<arr.length ; i ++){
            if(arr[i][key] == value){
                t.push(arr[i]);
            }
        }
        return t ;
    }




}
var NumberUtil = {
        parseNum:function (val, scale) {
            try{
                if(val.indexOf(',') >=0)val = val.replace(/,/g,"");
            }catch(e){}
            
            if (isNaN(val) == true || val == undefined || val == '') {
                return 0;
            }
            // 不设的话默认不带小数
            if (scale == null || scale == undefined) {
                scale = 2;
            }
            return this.parseFloatExtension(Math.round(val * Math.pow(10,scale)) / Math.pow(10,scale));
        },
        parseFloatExtension:function parseFloatExtension(val){
            return Math.round(parseFloat(val) * Math.pow(10,12))/Math.pow(10,12);
        }
} 


 //----工具类 开始----
        var ImportUtil = {};   

         ImportUtil.Binary = {
            fixdata(data) { //文件流转BinaryString
                var o = "",
                    l = 0,
                    w = 10240;
                for (; l < data.byteLength / w; ++l) o += String.fromCharCode.apply(null, new Uint8Array(data.slice(l * w, l * w + w)));
                o += String.fromCharCode.apply(null, new Uint8Array(data.slice(l * w)));
                return o;
            },
            s2ab(s) { //字符串转字符流
                var buf = new ArrayBuffer(s.length);
                var view = new Uint8Array(buf);
                for (var i = 0; i != s.length; ++i) view[i] = s.charCodeAt(i) & 0xFF;
                return buf;
            }
        }
        ImportUtil.XLSX = {
            wb: null,
            rABS: false,
            import(f, c) {//导入根据文件
                this.wb = null;
                var reader = new FileReader();
                reader.onload = function (e) {
                    var data = e.target.result;
                    if (ImportUtil.XLSX.rABS) {
                        ImportUtil.XLSX.wb = XLSX.read(btoa(ImportUtil.Binary.fixdata(data)), {//手动转化
                            type: 'base64'
                        });
                    } else {
                        ImportUtil.XLSX.wb = XLSX.read(data, {
                            type: 'binary'
                        });
                    }
                    if (c && typeof (c)) {
                        c();
                    }
                };
                if (ImportUtil.XLSX.rABS) {
                    reader.readAsArrayBuffer(f);
                } else {
                    reader.readAsBinaryString(f);
                }
            },
            onImport(obj, c) {//导入根据 input file标签
                if (!obj.files) {
                    return;
                }
                this.import(obj.files[0], c);
            },
            getSheetsByIndex(index = 0) {//获取数据根据Sheets索引
                return XLSX.utils.sheet_to_json(this.wb.Sheets[this.wb.SheetNames[index]]);
            },
            getCharCol(n) {
                let temCol = '',
                    s = '',
                    m = 0
                while (n > 0) {
                    m = n % 26 + 1
                    s = String.fromCharCode(m + 64) + s
                    n = (n - m) / 26
                }
                return s
            },
            export(json, title, type) {//导出
                var keyMap = [];//获取keys
                for (k in json[0]) {
                    keyMap.push(k);
                }
                var tmpdata = [];//用来保存转换好的json 
                json.map((v, i) => keyMap.map((k, j) => Object.assign({}, {
                    v: v[k],
                    position: (j > 25 ? ImportUtil.XLSX.getCharCol(j) : String.fromCharCode(65 + j)) + (i + 1)
                }))).reduce((prev, next) => prev.concat(next)).forEach((v, i) => tmpdata[v.position] = {
                    v: v.v
                });
                var outputPos = Object.keys(tmpdata); //设置区域,比如表格从A1到D10
                var tmpWB = new Object();
                title = title ? title : "工作表1";
                tmpWB.SheetNames = [title];
                tmpWB.Sheets = {};
                tmpWB.Sheets[title] = Object.assign({}, tmpdata, //内容
                    {
                        '!ref': outputPos[0] + ':' + outputPos[outputPos.length - 1] //设置填充区域
                    });
                return new Blob([ImportUtil.Binary.s2ab(XLSX.write(tmpWB,
                    { bookType: (type == undefined ? 'xlsx' : type), bookSST: false, type: 'binary' }//这里的数据是用来定义导出的格式类型
                ))], { type: "" }); //创建二进制对象写入转换好的字节流
            },
            onExport(json, title, type,fileName ) {//直接下载
                ImportUtil.Download.byObj(this.export(json, title, type), fileName);
            },
                  /* 组装excel表头
             * headerMap 表头 
             * return ws 表格
             */
            assebleExcelHeader(headerMap){
                return XLSX.utils.json_to_sheet([headerMap], {header: Object.keys(headerMap), skipHeader: true});
            },
             /* 组装excel 数据
             * ws 表格
             * headerMap 表头 
             * data 数据
             * origin 行起点
             * return ws 表格
             */
             assebleExcelData(ws,headerMap,data,origin){

                var keys = Object.keys(headerMap);
                var d = [];
                for (var i = 0; i<data.length; i++) {
                   var d1 =  data[i];
                   var t = {};
                   for(var j=0;j < keys.length ; j ++){

                      t[keys[j]] = d1[keys[j]] == null ? "": d1[keys[j]];
                   }
                   d.push(t);
                }
                return XLSX.utils.sheet_add_json(ws,d,{header: Object.keys(headerMap),skipHeader: true, origin: origin}); 
            },
             //下载数据
             /*
             * ws worksheet 
             *
             */
            doit(ws,title,fn, dl) {
                    var type = "xlsx";
                    var wb  = XLSX.utils.book_new();
                    XLSX.utils.book_append_sheet(wb, ws, title);
                    return dl ?
                        XLSX.write(wb, {bookType:type, bookSST:true, type: 'base64'}) :
                        XLSX.writeFile(wb, fn || (title+'.' + (type || 'xlsx')));
            },
            getDatagridHeaderMap(target,removes){
                var fields = $(target).datagrid('getColumnFields',true).concat($(target).datagrid('getColumnFields',false));   
                if(removes != null && removes.length > 0) fields = ArrayUtil.removes(fields,removes);
                var headerMap = {};     
                    for(var i = 0 ; i < fields.length ; i ++){
                        //if(fields[i] == 'ck')continue;
                        headerMap[fields[i]] = $(target).datagrid('getColumnOption',fields[i]).title
                    }
                return headerMap;

            },
            exportExcel (target ,data, headerMap ,title ){
                    //var d = $(target).datagrid('getData').rows
                    var ws = this.assebleExcelHeader(headerMap);
                    ws = this.assebleExcelData(ws,headerMap,data,"A2");
                    this.doit(ws,title);
            }


        };
        ImportUtil.Download = {
            byURL(url, fileName) {//动态下载
                var tmpa = document.createElement("a");
                tmpa.download = fileName || "下载";
                tmpa.href = url; //绑定a标签
                tmpa.click(); //模拟点击实现下载
            },
            byObj(obj, fileName) {//内存二进制数据下载
                this.byURL(URL.createObjectURL(obj), fileName);
                setTimeout(function () { //延时释放
                    URL.revokeObjectURL(obj); //用URL.revokeObjectURL()来释放这个object URL
                }, 100);
            }
        }
        ImportUtil.Object = {
            reverse(obj) {//对象键值倒转
                var o = new Object();
                for (var k in obj) {
                    o[obj[k]] = k;
                }
                return o;
            },
            deepCopy() {//深拷贝
                let temp = obj.constructor === Array ? [] : {}
                for (let val in obj) {
                    temp[val] = typeof obj[val] == 'object' ? deepCopy(obj[val]) : obj[val];
                }
                return temp;
            },
            copyJson(o) { return JSON.parse(JSON.stringify(o)); }//拷贝JSON格式
        }


   
    

      //////  

        
        
        
        



        

        